﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FormControllers;
using AgencyEntityLib.BaseEntity;
using Ecopsy.Tools;
using Ecopsy.DataTier;
using SqlLibrary;


/// <summary>
/// настройка - адресные программы / подразделение
/// Загрузить настройку - адресные программ подразделения / пометить галочками - чтоесть чего нет.
/// </summary>

public partial class iFarmeAdmin_AdressPrograms : FormAdressPrograms 
{
    
    wsDictionary ws_Dictionary = (wsDictionary)GlobalConstants.CurrentAgency.WS_FactoryList[WS_FactoryList.wsKey_Dictionary];

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            MyBind();

        }
    }

    

    protected void AddButton_Click(object sender, EventArgs e)
    {
        // если нет справочника - создать его
        wsDictionary _ws_Dictionary = (wsDictionary)GlobalConstants.CurrentAgency.WS_FactoryList[WS_FactoryList.wsKey_Dictionary];                
        TreeDictionaryItem d=  _ws_Dictionary.CreateTreeDictionary(GlobalConstants.TreeDictionaryFolderType_System, GlobalConstants.TreeDictionary_AdressProgram, "Новая адресная программа");
        Response.Redirect("~\\iFarmeDictionary_TreeDictonary.aspx?TreeDictionaryID=" + d.ID + "&FolderID=" + GlobalConstants.TreeDictionaryFolderType_System);  
    }
   
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {   
        MyBind();
    }
  
    #region Private
    private string TreeDictionaryType { get { return Request.QueryString["TreeDictionaryType"]; } }

    private void MyBind()
    {
        ObjectDS_F ODS = new ObjectDS_F(GetLvList, Where);
        grdAdrProgrаmms.DataSource = ODS.ImAsObjectDataSource;
        DataBind();
        SetSortCss();
    }
    protected void SetSortCss()
    {
        if (null == grdAdrProgrаmms.HeaderRow) return;

        foreach (TableCell cell in grdAdrProgrаmms.HeaderRow.Cells) cell.CssClass = string.Empty;        
        string css = (Where.SortCondition.SortDirection== SortDirection.Ascending ? "sortAscending" : "sortDescending");

        switch (Where.SortCondition.SortField)
        {
            case "Name":
                grdAdrProgrаmms.HeaderRow.Cells[0].CssClass = css;
                break;
            case "CreateDate":
                grdAdrProgrаmms.HeaderRow.Cells[1].CssClass = css;
                break;
            case "LastUpdate":
                grdAdrProgrаmms.HeaderRow.Cells[2].CssClass = css;
                break;            
        }


    }

    

    protected  Department _Departament   
    { 
        
        get {            
                return _wsAgency.GetDepartment(qDepartamentID_Value); 
        } 
    }

    public AdressPropramItem[] GetLvList(object _Where)
   {       
        List<AdressPropramItem> FullList= GlobalConstants .CurrentAgency .GetAdressPropramItems ();
        //AdressPropramItem[] r= FullList.ToArray<AdressPropramItem>();

        ListWhere w = (ListWhere)_Where;
        Where.SortCondition.fnOrderBy = SortFunctions.fOrderBy;

        // фильтруем по нам=именованию
        var result =
          from
              n in FullList
          where
            n.Name.ToUpper().IndexOf(txtSearch.Text.ToUpper()) > -1 || txtSearch.Text.Length ==0            
          select n;

        AdressPropramItem[] r = result.ToArray<AdressPropramItem>();
        Array.Sort(r, Where.SortCondition);
        return r;
   }

    private string FolderID
    {
        get { return GlobalConstants.TreeDictionaryFolderType_System; }
    }

    private string DictionaryTypeID
    {
        get { return GlobalConstants.TreeDictionary_AdressProgram ; }
    }

    #endregion
    protected void grdEmployeers_Sorting(object sender, GridViewSortEventArgs e)
    {
        Where.SortCondition.SortDirection = (Where.SortCondition.SortDirection == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending); ;
        Where.SortCondition.SortField = e.SortExpression;
        SetSortCss();
        MyBind();

    }
    protected void grdEmployeers_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }
   
    protected void BtnEdit_Click(object sender, EventArgs e)
    {
        Response.Redirect("~\\iFarmeDictionary_TreeDictonary.aspx?TreeDictionaryID=" + ((Button)sender).CommandArgument + "&FolderID=" + GlobalConstants.TreeDictionaryFolderType_System);  

    }


   public  class lvItem
    {
       private Department _d;
       public lvItem(Department d)
       {
           _d = d;

       }
       public bool Selected
       {
           get;
           set;
       }
        
        public string ID { get; set; }
        public string Name { get; set; }
        public DateTime  CreateDate { get; set; }
        public DateTime LastUpdate { get; set; }
        public  bool SetSelected(string AdrPrgId) 
        {
            return !(_d.Settings.AdressProgrmList[AdrPrgId] == null);  
        }
    }
   
   protected void btSave_Click(object sender, EventArgs e)
   {
       ////_Departament.Settings.AdressProgrmList.Clear();   
       //foreach (GridViewRow item in grdAdrProgrаmms.Rows)
       //{
       //    if (item.RowType == DataControlRowType.DataRow)
       //    {
       //        CheckBox d = (CheckBox)item.Cells[0].FindControl("chSelectedProgramm");
       //        HiddenField f = (HiddenField)item.Cells[0].FindControl("hfSelectedProgramm");

       //        Agency a = GlobalConstants.CurrentAgency;
       //        a.Set_UseAdressProgramm(f.Value,d.Checked);
       //    }

       //}
       //wsAgency ws = new wsAgency();
       //ws.SaveAgency();
       //MyBind();

       
   }
   protected void grdAdrProgrаmms_RowDataBound(object sender, GridViewRowEventArgs e)
   {
       if (e.Row.RowType == DataControlRowType.DataRow)
       {
           //if (e.Row.RowState == DataControlRowState.Normal)
           //{
           //    e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='"+GlobalConstants.rwBackGroundClr +"'");
           //    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='" + System.Drawing.ColorTranslator.ToHtml(grdAdrProgrаmms.RowStyle.BackColor) + "'");
               
           //}
           //else if (e.Row.RowState == DataControlRowState.Alternate)
           //{
           //    e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='" + GlobalConstants.rwBackGroundClr + "'");
           //    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='" + System.Drawing.ColorTranslator.ToHtml(grdAdrProgrаmms.AlternatingRowStyle.BackColor) + "'");
           //}
       }
   }
   protected void grdAdrProgrаmms_SelectedIndexChanged(object sender, EventArgs e)
   {

   }
}
